package es.deusto.herotactics.utils;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class FileUtils {
	// Devuelve una FileList con los nombres de los archivos del directorio especificado y la extension establecida
	public static File[] getFilesWithSpecificExtensionInDirectory(String directoryPath, final String extension)
	{
		if (directoryPath == null)
		{
			throw new NullPointerException("The given directory path string cannot be null.");
		}
		if (extension == null)
		{
			throw new NullPointerException("The given extension string cannot be null.");
		}
		File directoryPathFile = new File(directoryPath);
		if (!directoryPathFile.isDirectory()) 
		{
			throw new IllegalArgumentException("The given folder path '" + directoryPath + "' is not a directory.");
		}
		// Implement and instantiate a filename filter using an anonymous class.
		FilenameFilter filenameFilter = new FilenameFilter()
		{
			@Override
			public boolean accept(File dir, String name)
			{
				// The files with the extension in their names will be accepted in the filter.
				// It is case insensitive.
				return name.toUpperCase().endsWith(extension.toUpperCase());
			}
		}; 
		// Get the files using the filter.
		File[] filesWithSpecificExtension = directoryPathFile.listFiles(filenameFilter);
		return filesWithSpecificExtension;
	}
	
	// Convierte una FileList a un ArrayList, quitandole la extension a los nombres en caso de que la tuviesen
	public static ArrayList<String> deFileAArrayList (File[] files)
	{
		ArrayList<String> list = new ArrayList<String>();
		StringTokenizer st;
		if (files != null)
		{
			for (int i=0; i<files.length; i++)
			{
				File f = files[i];
				st = new StringTokenizer(f.getName(), ".");
				list.add(st.nextToken());
			}
		}
		return list;
	}
}
